package de.wz.schwimmen;

import java.io.PrintWriter;
import java.io.StringWriter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

/**
 * @author Peter Eimann
 */
public class HistoryActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread paramThread, Throwable exception) {
                StringWriter stackTrace = new StringWriter();
                exception.printStackTrace(new PrintWriter(stackTrace));

                StringBuilder errormessage = new StringBuilder(exception.getMessage());
            	while (exception.getCause() != null) {
            		exception = exception.getCause();
            		errormessage.append("\n\t\t- ").append(exception.getMessage());
            	}

                Intent spielIntent = new Intent(HistoryActivity.this, SpielActivity.class);
                spielIntent.putExtra("de.wz.schwimmen.errormessage", errormessage.toString());
                spielIntent.putExtra("de.wz.schwimmen.stacktrace", stackTrace.toString());
                spielIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                spielIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(spielIntent);
                System.exit(0);
            }
        });

		setContentView(R.layout.history);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}

	@Override
	public void onStart() {
		super.onStart();

		int index = 1;
		int[] historyItem = null;
		TableLayout table = (TableLayout)findViewById(R.id.tableLayoutHistory);

		Bundle basket = getIntent().getExtras();
		do {
			historyItem = basket.getIntArray("history_item" + index);
			if (historyItem != null) {
				addTableRow(index, historyItem, table);
				index++;
			}
		} while (historyItem != null);
	}

	private void addTableRow(int index, int[] historyItem, TableLayout table) {
		TableRow row = new TableRow(this);
		row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

		row.addView(createTextView(index + ".", true));
		row.addView(createTextView(Integer.toString(historyItem[0]), false));
		row.addView(createTextView(Integer.toString(historyItem[1]), false));
		row.addView(createTextView(Integer.toString(historyItem[2]), false));
		row.addView(createTextView(Integer.toString(historyItem[3]), false));

		table.addView(row);
	}

	private TextView createTextView(String text, boolean bold) {
		TextView view = new TextView(this);
		view.setText(text);
		view.setGravity(Gravity.CENTER);
		view.setTextSize(22);
		view.setTextAppearance(this, android.R.style.TextAppearance_Large);
		if (bold) {
			view.setTypeface(null, Typeface.BOLD);
		}

		return view;
	}
} 